package com.cn.cs;

import java.sql.*;

public class DBUtils {
    //封装连接数据库的参数信息
    private  static  final String CLASSNAME="com.mysql.jdbc.Driver";
    private static  final  String USERNAME="root";
    private static  final  String PASSWORD="root";
    private static  final  String URL="jdbc:mysql://localhost:3306/mysql1";
    //加载驱动
    static{
        try {
            Class.forName(CLASSNAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /****
     * 获取连接对象并且返回给别人去用
     * @return
     * @throws SQLException
     */
    public static Connection openConnection() throws SQLException{
        Connection con =DriverManager.getConnection(URL,USERNAME,PASSWORD);
        return con;
    }
    /****
     * 使用完成之后需要关闭资源
     */
    public static void close(Connection con, PreparedStatement pst,ResultSet rs) throws SQLException{
        if(rs!=null)
            rs.close();
        if(pst!=null)
            pst.close();
        if(con!=null)
            con.close();
    }
}